#ifndef _NET_REQUEST_HPP_
#define _NET_REQUEST_HPP_

#include <boost/asio.hpp>
#include <sstream>
#include <memory>

class req_t {
public:

  req_t()
    : stream_(new std::ostringstream)
    , buf_(new std::vector<char>)
  {}

  template <typename T>
  req_t& add( const T &val) {
    *stream_ << val;
    return *this;
  }

  void data(const char *data, size_t size) {
    clone_buf_from_stream();
    ptrdiff_t len = buf_->size();
    buf_->resize(len+size);
    memcpy(&((*buf_)[0])+len, data, size);
  }

  const std::vector<char> &buf() {
    if (buf_->empty()) {
      clone_buf_from_stream();
    }
    return *buf_;
  }


private:

  void clone_buf_from_stream() {
    std::string tmp = stream_->str();
    const char *ptr = tmp.data();
    ptrdiff_t len = tmp.length();
    buf_->resize(len);
    buf_->assign(ptr, ptr + len);
  }

  boost::shared_ptr<std::ostringstream> stream_;
  boost::shared_ptr<std::vector<char> > buf_;
};


#endif //_NET_REQUEST_HPP_
